{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Preparing data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Fare</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3</td>\n",
       "      <td>male</td>\n",
       "      <td>22.0</td>\n",
       "      <td>1</td>\n",
       "      <td>7.2500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>female</td>\n",
       "      <td>38.0</td>\n",
       "      <td>1</td>\n",
       "      <td>71.2833</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>female</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0</td>\n",
       "      <td>7.9250</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Pclass     Sex   Age  SibSp     Fare\n",
       "0       3    male  22.0      1   7.2500\n",
       "1       1  female  38.0      1  71.2833\n",
       "2       3  female  26.0      0   7.9250"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import catboost\n",
    "from catboost.datasets import titanic\n",
    "\n",
    "titanic_train, titanic_test = titanic()\n",
    "titanic_train_target = titanic_train.Survived\n",
    "\n",
    "titanic_train.drop(['PassengerId', 'Survived', 'Name', 'Parch', 'Ticket', 'Cabin', 'Embarked'], axis=1, inplace=True)\n",
    "titanic_test.drop(['PassengerId', 'Name', 'Parch', 'Ticket', 'Cabin', 'Embarked'], axis=1, inplace=True)\n",
    "\n",
    "titanic_train.head(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Simple grid search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "titanic_model = catboost.CatBoostClassifier(\n",
    "    iterations=1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_pool = catboost.Pool(titanic_train, titanic_train_target, cat_features=['Pclass', 'Sex', 'SibSp'])\n",
    "test_pool = catboost.Pool(titanic_test, cat_features=['Pclass', 'Sex', 'SibSp'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0:\tloss: 0.3602654\tbest: 0.3602654 (0)\ttotal: 26.9s\tremaining: 12m 58s\n",
      "1:\tloss: 0.3494103\tbest: 0.3494103 (1)\ttotal: 54.4s\tremaining: 12m 42s\n",
      "2:\tloss: 0.3641982\tbest: 0.3494103 (1)\ttotal: 1m 22s\tremaining: 12m 22s\n",
      "3:\tloss: 0.3607842\tbest: 0.3494103 (1)\ttotal: 1m 51s\tremaining: 12m 1s\n",
      "4:\tloss: 0.3614755\tbest: 0.3494103 (1)\ttotal: 2m 19s\tremaining: 11m 36s\n",
      "5:\tloss: 0.3688638\tbest: 0.3494103 (1)\ttotal: 2m 47s\tremaining: 11m 9s\n",
      "6:\tloss: 0.3661836\tbest: 0.3494103 (1)\ttotal: 3m 15s\tremaining: 10m 42s\n",
      "7:\tloss: 0.3589304\tbest: 0.3494103 (1)\ttotal: 3m 44s\tremaining: 10m 16s\n",
      "8:\tloss: 0.3679994\tbest: 0.3494103 (1)\ttotal: 4m 11s\tremaining: 9m 45s\n",
      "9:\tloss: 0.3648804\tbest: 0.3494103 (1)\ttotal: 4m 36s\tremaining: 9m 13s\n",
      "10:\tloss: 0.3544873\tbest: 0.3494103 (1)\ttotal: 5m 5s\tremaining: 8m 47s\n",
      "11:\tloss: 0.3730255\tbest: 0.3494103 (1)\ttotal: 5m 34s\tremaining: 8m 21s\n",
      "12:\tloss: 0.3527641\tbest: 0.3494103 (1)\ttotal: 6m 2s\tremaining: 7m 54s\n",
      "13:\tloss: 0.3589757\tbest: 0.3494103 (1)\ttotal: 6m 30s\tremaining: 7m 26s\n",
      "14:\tloss: 0.3613885\tbest: 0.3494103 (1)\ttotal: 6m 58s\tremaining: 6m 58s\n",
      "15:\tloss: 0.3617778\tbest: 0.3494103 (1)\ttotal: 7m 28s\tremaining: 6m 32s\n",
      "16:\tloss: 0.3611816\tbest: 0.3494103 (1)\ttotal: 7m 57s\tremaining: 6m 4s\n",
      "17:\tloss: 0.3558780\tbest: 0.3494103 (1)\ttotal: 8m 25s\tremaining: 5m 37s\n",
      "18:\tloss: 0.3606356\tbest: 0.3494103 (1)\ttotal: 8m 55s\tremaining: 5m 9s\n",
      "19:\tloss: 0.3647560\tbest: 0.3494103 (1)\ttotal: 9m 22s\tremaining: 4m 41s\n",
      "20:\tloss: 0.3556906\tbest: 0.3494103 (1)\ttotal: 10m 37s\tremaining: 4m 33s\n",
      "21:\tloss: 0.3558181\tbest: 0.3494103 (1)\ttotal: 12m 5s\tremaining: 4m 23s\n",
      "22:\tloss: 0.3543031\tbest: 0.3494103 (1)\ttotal: 13m 17s\tremaining: 4m 2s\n",
      "23:\tloss: 0.3571505\tbest: 0.3494103 (1)\ttotal: 14m 50s\tremaining: 3m 42s\n",
      "24:\tloss: 0.3554287\tbest: 0.3494103 (1)\ttotal: 16m 8s\tremaining: 3m 13s\n",
      "25:\tloss: 0.3634387\tbest: 0.3494103 (1)\ttotal: 17m 42s\tremaining: 2m 43s\n",
      "26:\tloss: 0.3604475\tbest: 0.3494103 (1)\ttotal: 18m 36s\tremaining: 2m 4s\n",
      "27:\tloss: 0.3672894\tbest: 0.3494103 (1)\ttotal: 19m 39s\tremaining: 1m 24s\n",
      "28:\tloss: 0.3647169\tbest: 0.3494103 (1)\ttotal: 20m 32s\tremaining: 42.5s\n",
      "29:\tloss: 0.3674487\tbest: 0.3494103 (1)\ttotal: 21m 34s\tremaining: 0us\n",
      "Estimating final quality...\n"
     ]
    }
   ],
   "source": [
    "grid = {\n",
    "    'learning_rate': [0.03, 0.1],\n",
    "    'depth':[4, 6, 10],\n",
    "    'l2_leaf_reg': [1, 3, 5, 7, 9]\n",
    "}\n",
    "grid_search_results = titanic_model.grid_search(grid, train_pool, shuffle=False, verbose=3, plot=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Parameters giving the best value of the loss function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'depth': 4, 'l2_leaf_reg': 1, 'learning_rate': 0.1}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grid_search_results['params']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Available cross-validation statistics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['test-Logloss-mean', 'test-Logloss-std', 'train-Logloss-mean', 'train-Logloss-std', 'iterations'])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grid_search_results['cv_results'].keys()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Quality estimated using cross-validation:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.4919203237312826"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grid_search_results['cv_results']['test-Logloss-mean'][-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Model is ready to use after searching:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8.99675512e-01, 1.00324488e-01],\n",
       "       [9.99798150e-01, 2.01850144e-04],\n",
       "       [7.35134536e-01, 2.64865464e-01]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predicted = titanic_model.predict_proba(test_pool)\n",
    "predicted[:3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Searching over several grids"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Grid #0\n",
      "0:\tloss: 0.3822197\tbest: 0.3822197 (0)\ttotal: 25.8s\tremaining: 4m 44s\n",
      "1:\tloss: 0.3643472\tbest: 0.3643472 (1)\ttotal: 50.6s\tremaining: 4m 13s\n",
      "2:\tloss: 0.3636651\tbest: 0.3636651 (2)\ttotal: 1m 14s\tremaining: 3m 42s\n",
      "3:\tloss: 0.3690042\tbest: 0.3636651 (2)\ttotal: 1m 36s\tremaining: 3m 13s\n",
      "4:\tloss: 0.3602654\tbest: 0.3602654 (4)\ttotal: 2m 2s\tremaining: 2m 51s\n",
      "5:\tloss: 0.3494103\tbest: 0.3494103 (5)\ttotal: 2m 28s\tremaining: 2m 28s\n",
      "6:\tloss: 0.3334654\tbest: 0.3334654 (6)\ttotal: 2m 53s\tremaining: 2m 3s\n",
      "7:\tloss: 0.4026886\tbest: 0.3334654 (6)\ttotal: 3m 17s\tremaining: 1m 38s\n",
      "8:\tloss: 0.3625327\tbest: 0.3334654 (6)\ttotal: 3m 44s\tremaining: 1m 14s\n",
      "9:\tloss: 0.3500318\tbest: 0.3334654 (6)\ttotal: 4m 10s\tremaining: 50.2s\n",
      "10:\tloss: 0.3486949\tbest: 0.3334654 (6)\ttotal: 4m 35s\tremaining: 25.1s\n",
      "11:\tloss: 0.3626046\tbest: 0.3334654 (6)\ttotal: 4m 59s\tremaining: 0us\n",
      "Grid #1\n",
      "0:\tloss: 0.3603296\tbest: 0.3603296 (0)\ttotal: 26.1s\tremaining: 4m 47s\n",
      "1:\tloss: 0.3568793\tbest: 0.3568793 (1)\ttotal: 52.3s\tremaining: 4m 21s\n",
      "2:\tloss: 0.3582434\tbest: 0.3568793 (1)\ttotal: 1m 17s\tremaining: 3m 51s\n",
      "3:\tloss: 0.3675278\tbest: 0.3568793 (1)\ttotal: 1m 40s\tremaining: 3m 20s\n",
      "4:\tloss: 0.3573510\tbest: 0.3568793 (1)\ttotal: 2m 6s\tremaining: 2m 57s\n",
      "5:\tloss: 0.3576773\tbest: 0.3568793 (1)\ttotal: 2m 33s\tremaining: 2m 33s\n",
      "6:\tloss: 0.3555242\tbest: 0.3555242 (6)\ttotal: 2m 58s\tremaining: 2m 7s\n",
      "7:\tloss: 0.3816237\tbest: 0.3555242 (6)\ttotal: 3m 21s\tremaining: 1m 40s\n",
      "8:\tloss: 0.3607387\tbest: 0.3555242 (6)\ttotal: 3m 47s\tremaining: 1m 15s\n",
      "9:\tloss: 0.3564284\tbest: 0.3555242 (6)\ttotal: 4m 14s\tremaining: 50.9s\n",
      "10:\tloss: 0.3772445\tbest: 0.3555242 (6)\ttotal: 4m 38s\tremaining: 25.3s\n",
      "11:\tloss: 0.3791224\tbest: 0.3555242 (6)\ttotal: 5m 1s\tremaining: 0us\n",
      "Estimating final quality...\n"
     ]
    }
   ],
   "source": [
    "grid_1 = {\n",
    "    'learning_rate': [0.03, 0.1, 0.3, 0.9],\n",
    "    'bootstrap_type':['Bayesian'],\n",
    "    'bagging_temperature': [0, 1, 10]\n",
    "}\n",
    "grid_2 = {\n",
    "    'learning_rate': [0.03, 0.1, 0.3, 0.9],\n",
    "    'bootstrap_type':['Bernoulli'],\n",
    "    'subsample': [0.66, 0.7, 0.8]\n",
    "}\n",
    "grid_search_results = titanic_model.grid_search([grid_1, grid_2], train_pool, shuffle=False, verbose=4, plot=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'bootstrap_type': 'Bernoulli', 'learning_rate': 0.3, 'subsample': 0.7}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grid_search_results['params']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Randomized search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0:\tloss: 0.3750468\tbest: 0.3750468 (0)\ttotal: 23.6s\tremaining: 4m 19s\n",
      "1:\tloss: 0.6544126\tbest: 0.3750468 (0)\ttotal: 39.3s\tremaining: 3m 16s\n",
      "2:\tloss: 0.3748575\tbest: 0.3748575 (2)\ttotal: 1m 3s\tremaining: 3m 11s\n",
      "3:\tloss: 0.6544129\tbest: 0.3748575 (2)\ttotal: 1m 19s\tremaining: 2m 39s\n",
      "4:\tloss: 0.3746511\tbest: 0.3746511 (4)\ttotal: 1m 46s\tremaining: 2m 28s\n",
      "5:\tloss: 0.4227691\tbest: 0.3746511 (4)\ttotal: 2m 7s\tremaining: 2m 7s\n",
      "6:\tloss: 0.3649895\tbest: 0.3649895 (6)\ttotal: 2m 34s\tremaining: 1m 50s\n",
      "7:\tloss: 0.6544129\tbest: 0.3649895 (6)\ttotal: 2m 50s\tremaining: 1m 25s\n",
      "8:\tloss: 0.3717568\tbest: 0.3649895 (6)\ttotal: 3m 16s\tremaining: 1m 5s\n",
      "9:\tloss: 0.3705661\tbest: 0.3649895 (6)\ttotal: 3m 41s\tremaining: 44.4s\n",
      "10:\tloss: 0.3680459\tbest: 0.3649895 (6)\ttotal: 4m 9s\tremaining: 22.7s\n",
      "11:\tloss: 0.4246879\tbest: 0.3649895 (6)\ttotal: 4m 32s\tremaining: 0us\n",
      "Estimating final quality...\n"
     ]
    }
   ],
   "source": [
    "from scipy import stats\n",
    "\n",
    "class StrangeDistribution:\n",
    "    def __init__(self, values):\n",
    "        self.values = values\n",
    "\n",
    "    def rvs(self):\n",
    "        return self.values[0]\n",
    "\n",
    "param_distribution = {\n",
    "    'one_hot_max_size': stats.bernoulli(p=0.2, loc=2),\n",
    "    'learning_rate': StrangeDistribution([0.03, 0.1]),\n",
    "    'l2_leaf_reg': [1, 3, 5, 7, 9],\n",
    "    'depth': stats.binom(n=10, p=0.2)\n",
    "}\n",
    "\n",
    "randomized_search_results = titanic_model.randomized_search(\n",
    "    param_distribution,\n",
    "    train_pool,\n",
    "    n_iter=12,\n",
    "    shuffle=False,\n",
    "    plot=True\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'depth': 4.0,\n",
       " 'l2_leaf_reg': 5,\n",
       " 'learning_rate': 0.03,\n",
       " 'one_hot_max_size': 2.0}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "randomized_search_results['params']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0:\tloss: 0.4434842\tbest: 0.4434842 (0)\ttotal: 2m 7s\tremaining: 10m 39s\n",
      "1:\tloss: 0.4402693\tbest: 0.4402693 (1)\ttotal: 4m 19s\tremaining: 8m 38s\n",
      "2:\tloss: 0.4644695\tbest: 0.4402693 (1)\ttotal: 6m 30s\tremaining: 6m 30s\n",
      "3:\tloss: 0.4422576\tbest: 0.4402693 (1)\ttotal: 8m 48s\tremaining: 4m 24s\n",
      "4:\tloss: 0.6659128\tbest: 0.4402693 (1)\ttotal: 10m 38s\tremaining: 2m 7s\n",
      "5:\tloss: 0.4351239\tbest: 0.4351239 (5)\ttotal: 14m 12s\tremaining: 0us\n"
     ]
    }
   ],
   "source": [
    "# If search_by_train_test_split=False, every iteration of grid search evaluates results on cross-validation.\n",
    "randomized_search_results = titanic_model.randomized_search(\n",
    "    param_distribution,\n",
    "    train_pool,\n",
    "    n_iter=6,\n",
    "    shuffle=False,\n",
    "    search_by_train_test_split=False,\n",
    "    plot=True\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'depth': 3.0,\n",
       " 'l2_leaf_reg': 7,\n",
       " 'learning_rate': 0.03,\n",
       " 'one_hot_max_size': 3.0}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "randomized_search_results['params']"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
